package org.fangzz.cameramgr.cameramonitor.rest;

import org.fangzz.cameramgr.cameramonitor.dto.CameraSummary;
import org.fangzz.cameramgr.cameramonitor.model.Camera;
import org.fangzz.cameramgr.cameramonitor.service.CameraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * created by XxChen 2018/6/22
 */

@RestController
public class StoreCameraRestController {

    @Autowired
    private CameraService cameraService;

    /**
     * 查询门店下所有摄像头
     * @param storeId
     * @return
     */

    @GetMapping("/api/camera/{storeId}")
    public List<CameraSummary> findByStore(@PathVariable Integer storeId){
        List<Camera> cameras = cameraService.findByStoreId(storeId);
        return CameraSummary.convert(cameras);
    }

    /**
     * 获取门店下所有摄像头并按区域分组
     * @param storeId
     * @return
     */

    @GetMapping("/api/camera/area/{storeId}")
    public Map<String,List<CameraSummary>> groupCamera(@PathVariable Integer storeId){
        List<Camera> cameras = cameraService.findByStoreId(storeId);
        List<CameraSummary> list = CameraSummary.convert(cameras);
        return list.stream().collect(Collectors.groupingBy(CameraSummary::getArea));
    }

    /**
     * 获取摄像头直播地址
     * @param cameraId
     * @return
     */
    @GetMapping("/api/camera/live/{cameraId}")
    public CameraSummary getLiveAddress(@PathVariable Integer cameraId){
        return CameraSummary.convert(cameraService.findById(cameraId));
    }



}
